package cc.linker.steplink.crm.merchant.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 剩余天数
	 * @param dateTargetStr
	 * @return
	 * @throws ParseException 
	 */
	public static int daysLeft(String dateTargetStr) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date soure = sdf.parse(dateTargetStr);
		Date now = new Date();
		return (int)((soure.getTime() - now.getTime())/(1000*60*60*24));
	}

	/**
	 * 剩余分钟数
	 * @param currentDate
	 * @return
	 */
	public static Integer getRemainSecondsOneDay(Date currentDate) {
		Calendar midnight=Calendar.getInstance();
		midnight.setTime(currentDate);
		midnight.add(Calendar.DAY_OF_MONTH,1);
		midnight.set(Calendar.HOUR_OF_DAY,0);
		midnight.set(Calendar.MINUTE,0);
		midnight.set(Calendar.SECOND,0);
		midnight.set(Calendar.MILLISECOND,0);
		Integer seconds=(int)((midnight.getTime().getTime()-currentDate.getTime())/1000);
		return seconds;

	}

	public static String dateStr(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static String dateStr(Date date , String pattern){
		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static String dataStr(String format, Long time){
		String date = new java.text.SimpleDateFormat(format).
				format(new java.util.Date(time * 1000));
		return date;
	}
	public static String getCurrDate(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date);
		return dateStr;
	}
	public static Date getFullDate(String str, String pattern) {
	     try {
	         SimpleDateFormat format = new SimpleDateFormat(pattern);
	         Date date = format.parse(str.trim());
	         return date;
	     } catch (Exception ex) {
	         ex.printStackTrace();
	     }
	     return null;
	}
	  /**
     * 日期加减操作
     * @param source 源日期
     * @param num 推迟天数 + 为往后 - 为往前
     * @return
     */
    public static Date dateRoler(Date source, int num) {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(source);
            c.add(Calendar.DAY_OF_MONTH, num);
            return c.getTime();
        } catch (Exception e) {
            return null;
        }
    }

	/**
	 * 日期加减操作
	 * @param source 源日期
	 * @param num 推迟天数 + 为往后 - 为往前
	 * @return
	 */
	public static Date miniuteRoler(Date source, int num) {
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(source);
			c.add(Calendar.MINUTE, num);
			return c.getTime();
		} catch (Exception e) {
			return null;
		}
	}
    //节目单相差1-30分钟预警
    public static Boolean dateDiff(String startTime, String endTime,
                                   String format, String str) {   
        // 按照传入的格式生成一个simpledateformate对象   
        SimpleDateFormat sd = new SimpleDateFormat(format);   
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数   
        long nh = 1000 * 60 * 60;// 一小时的毫秒数   
        long nm = 1000 * 60;// 一分钟的毫秒数   
        long ns = 1000;// 一秒钟的毫秒数   
        long diff;   
        long day = 0;   
        long hour = 0;   
        long min = 0;   
        long sec = 0;   
        // 获得两个时间的毫秒时间差异   
        try {   
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();   
            day = diff / nd;// 计算差多少天   
            hour = diff % nd / nh + day * 24;// 计算差多少小时   
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟   
            sec = diff % nd % nh % nm / ns;// 计算差多少秒   
            // 输出结果   
            if (str.equalsIgnoreCase("h")) {   
            	if("00:00".equals(startTime)){return true;}
            	if(0==hour){
            		if((0<min&&min<30)||(0>min&&min>-30)){
                		return false;
                	}else{
                		return true;
                		}  
            	}else{
            		return true;
            		} 
            } else {   
            	if("00:00".equals(startTime)){return true;}
            	if(0==hour){
            		if((0<min&&min<30)||(0>min&&min>-30)){
                		return false;
                	}else{
                		return true;
                		}  
            	}else{
            		return true;
            		} 
            }   
  
        } catch (ParseException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        }   
        if (str.equalsIgnoreCase("h")) {   
        	if("00:00".equals(startTime)){return true;}
        	if(0==hour){
        		if((0<min&&min<30)||(0>min&&min>-30)){
            		return false;
            	}else{
            		return true;
            		}  
        	}else{
        		return true;
        		}
        } else {   
        	if("00:00".equals(startTime)){return true;}
        	if(0==hour){
        		if((0<min&&min<30)||(0>min&&min>-30)){
            		return false;
            	}else{
            		return true;
            		}  
        	}else{
        		return true;
        		} 
        }   
    }
    
	/**
	 * 判断时间是否在时间段内
	 * 
	 * @param date
	 *            当前时间 yyyy-MM-dd HH:mm:ss
	 * @param strDateBegin
	 *            开始时间 00:00:00
	 * @param strDateEnd
	 *            结束时间 00:05:00
	 * @return
	 */
	public static boolean isInDate(String nowdate, String star, String end, String format){
		SimpleDateFormat localTime=new SimpleDateFormat(format);
		try{
			Date sdate = localTime.parse(star);
			Date edate=localTime.parse(end);
			Date date=localTime.parse(nowdate);
			if(date.after(sdate)&& date.before(edate)){
				return false;
			}else if(date.getTime()==sdate.getTime()||date.getTime()==edate.getTime()){
				return false;
			}else{
				return true;
			}
		}catch(Exception e){
			return false;
		}
		
	}


    public static void main(String[] args) {
    	
    	String str = DateUtil.dateStr(new Date());
    	String s = DateUtil.getCurrDate(new Date());
    	Date d = DateUtil.dateRoler(new Date(), -6);
    	String ss = DateUtil.getCurrDate(d);
    	
//    	Boolean ss=isInDate("2016-08-25","2016-08-22","2016-08-28","yyyy-MM-dd");
    	System.out.println(ss);
    }  
}
